using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public static class SaveSystem
{
    public static void Save(string fileName, object data)
    {
        var json = JsonUtility.ToJson(data);
        var path = Path.Combine(Application.persistentDataPath, fileName);

        try
        {
            Debug.Log(path);
            File.WriteAllText(path, json);
            #if UNITY_EDITOR
            Debug.Log($"Success save data to {path}.");
            #endif
        }
        catch(System.Exception exception)
        {
            #if UNITY_EDITOR
            Debug.LogError($"Fail to save data to {path}.\n{exception}");
            #endif
        }
    }

    public static T Load<T>(string fileName)
    {
        var path = Path.Combine(Application.persistentDataPath, fileName);

        try
        {
            var json = File.ReadAllText(path);
            var data = JsonUtility.FromJson<T>(json);
            return data;
        }
        catch(System.Exception exception)
        {
            #if UNITY_EDITOR
            Debug.LogError($"Fail to save data from {path}.\n{exception}");
            #endif
            return default;
        }
    }

    public static void DeleteSaveFile(string fileName)
    {
        var path = Path.Combine(Application.persistentDataPath, fileName);

        try
        {
            File.Delete(path);
        }
        catch(System.Exception exception)
        {
            #if UNITY_EDITOR
            Debug.LogError($"Fail to delete {path}.\n{exception}");
            #endif
        }
    }

    public static bool SaveFileExit(string fileName)
    {
        return File.Exists(Path.Combine(Application.persistentDataPath, fileName));
    }
}
